package cn.com.aiouyi.entity;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.AttributeConverter;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Converter;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;

import cn.com.aiouyi.common.BaseAttributeConverter;
import lombok.Getter;
import lombok.Setter;

/**
 * Entity - 插件配置
 */
@Getter
@Setter
@Entity
@Table(name = "t_plugin_config")
public class PluginConfig extends SeqEntity<Long> {

	private static final long serialVersionUID = -3703072378069785812L;

	/** 插件ID */
	@Column(nullable = false, updatable = false, unique = true)
	private String pluginId;

	/** 是否启用 */
	@Column(nullable = false, columnDefinition = "bit default false")
	private Boolean isEnabled;

	/** 属性 */
	@Column(length = 4000)
	@Convert(converter = MapConverter.class)
	private Map<String, String> attributes = new HashMap<String, String>();

	/**
	 * 获取属性值
	 *
	 * @param name
	 *            属性名称
	 * @return 属性值
	 */
	@Transient
	public String getAttribute(String name) {
		if (StringUtils.isEmpty(name)) {
			return null;
		}
		return getAttributes() != null ? getAttributes().get(name) : null;
	}

	/**
	 * 类型转换 - Map
	 *
	 * @author SHOP++ Team
	 * @version 4.0
	 */
	@Converter
	public static class MapConverter extends BaseAttributeConverter<Map<String, String>> implements AttributeConverter<Object, String> {
	}
}
